﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace SLGuideSystem
{
    public class GuideHoleMesh : MaskableGraphic,ICanvasRaycastFilter
    {
        public Vector2 FullMin;
        public Vector2 FullMax;
        public Vector2 HoleMin;
        public Vector2 HoleMax;

        protected override void Awake()
        {
            base.Awake();
            FullMin = Vector2.zero;
            FullMax = new Vector2(Screen.width, Screen.height);
            rectTransform.sizeDelta = FullMax;
        }

        public void SetHole(Vector2 holeMin, Vector2 holeMax)
        {
            HoleMin = holeMin;
            HoleMax = holeMax;
            
            SetVerticesDirty();
        }

        public void SetHole(RectTransform trans)
        {
            var rect = trans.rect;
            Debug.Log(rect);
            Debug.Log(trans.anchoredPosition);

            SetHole(rect.position-rect.size/2, rect.position + rect.size / 2);
        }

        public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
        {
            sp /= canvas.scaleFactor;
            if (sp.x > FullMin.x
                && sp.x < FullMax.x
                && sp.y > FullMin.y
                && sp.y < FullMax.y
                )
            {
                if (sp.x > HoleMin.x
                && sp.x <= HoleMax.x
                && sp.y > HoleMin.y
                && sp.y <= HoleMax.y)
                {
                    return false;
                }
            }
            return true;
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            CreateHoleMesh(vh, FullMin, FullMax, HoleMin, HoleMax);
        }




        void CreateHoleMesh(VertexHelper vertexHelper, Vector2 fullMin, Vector2 fullMax, Vector2 holeMin, Vector2 holeMax)
        {
            vertexHelper.Clear();
            Vector2 holeLeftTop = new Vector2(holeMin.x, holeMax.y);
            Vector2 holeRightBottom = new Vector2(holeMax.x, holeMin.y);

            Vector2 fullLeftTop = new Vector2(fullMin.x, fullMax.y);
            Vector2 fullRightBottom = new Vector2(fullMax.x, fullMin.y);

            Vector2 fullLeftInteral = new Vector2(fullMin.x, holeLeftTop.y);
            Vector2 fullTopInteral = new Vector2(holeMax.x, fullMax.y);
            Vector2 fullRightInteral = new Vector2(fullMax.x, holeRightBottom.y);
            Vector2 fullBottomInteral = new Vector2(holeMin.x, fullMin.y);

            vertexHelper.AddVert(fullMin, color, Vector4.zero);
            vertexHelper.AddVert(fullLeftInteral, color, new Vector4(0, 0.75f, 0, 0));
            vertexHelper.AddVert(holeLeftTop, color, new Vector4(0.25f, 0.75f, 0, 0));
            vertexHelper.AddVert(fullBottomInteral, color, new Vector4(0.25f, 0, 0, 0));

            vertexHelper.AddTriangle(0, 1, 2);
            vertexHelper.AddTriangle(2, 3, 0);


            vertexHelper.AddVert(fullLeftTop, color, new Vector4(0, 1, 0, 0));
            vertexHelper.AddVert(fullTopInteral, color, new Vector4(0.75f, 1, 0, 0));
            vertexHelper.AddVert(holeMax, color, new Vector4(0.75f, 0.75f, 0, 0));
            vertexHelper.AddVert(fullLeftInteral, color, new Vector4(0, 0.75f, 0, 0));

            vertexHelper.AddTriangle(4, 5, 6);
            vertexHelper.AddTriangle(6, 7, 4);

            vertexHelper.AddVert(fullMax, color, new Vector4(1, 1, 0, 0));
            vertexHelper.AddVert(fullRightInteral, color, new Vector4(1, 0.25f, 0, 0));
            vertexHelper.AddVert(holeRightBottom, color, new Vector4(0.75f, 0.25f, 0, 0));
            vertexHelper.AddVert(fullTopInteral, color, new Vector4(0.75f, 1, 0, 0));

            vertexHelper.AddTriangle(8, 9, 10);
            vertexHelper.AddTriangle(10, 11, 8);

            vertexHelper.AddVert(fullRightBottom, color, new Vector4(1, 0, 0, 0));
            vertexHelper.AddVert(fullBottomInteral, color, new Vector4(0.25f, 0, 0, 0));
            vertexHelper.AddVert(holeMin, color, new Vector4(0.25f, 0.25f, 0, 0));
            vertexHelper.AddVert(fullRightInteral, color, new Vector4(1, 0.25f, 0, 0));

            vertexHelper.AddTriangle(12, 13, 14);
            vertexHelper.AddTriangle(14, 15, 12);
        }
    }
}